package com.asset.dispoal.file.evidenceRecognition.service.commom;

import com.asset.dispoal.business.bsEvidenceRecognition.domain.db.mapper.BusinessDataManageMapper;
import com.asset.dispoal.file.evidenceRecognition.domain.db.entity.EvidenceRecognition;
import com.asset.dispoal.file.evidenceRecognition.domain.db.mapper.EvidenceRecognitionMapper;
import com.asset.dispoal.file.evidenceRecognition.dto.EvidenceRecognitionRequest;
import com.asset.dispoal.file.evidenceRecognition.service.converter.FileEvidenceRecognitionConverter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import tech.mhuang.pacebox.core.ddd.BaseCommand;
import tech.mhuang.pacebox.core.id.BaseIdeable;
import tech.mhuang.pacebox.core.util.CollectionUtil;
import tech.mhuang.pacebox.springboot.core.local.GlobalHeaderThreadLocal;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author qr
 * @date 2021/11/23$
 */
@Component
public class FileEvidenceRecognitionCommand implements BaseCommand<EvidenceRecognitionRequest, Boolean> {

    @Autowired
    EvidenceRecognitionMapper evidenceRecognitionMapper;

    @Autowired
    BusinessDataManageMapper businessDataManageMapper;
    @Autowired
    BaseIdeable<String> snowFlake;


    @Override
    @Transactional(propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean execute(EvidenceRecognitionRequest evidenceRecognitionRequest) {
        String loginUser = GlobalHeaderThreadLocal.getOrException().getUserId();
        EvidenceRecognition evidenceRecognitionDO = FileEvidenceRecognitionConverter.INSTANCE.requestToDo(evidenceRecognitionRequest);
        evidenceRecognitionDO.setCreateUser(loginUser);
        evidenceRecognitionDO.setId(snowFlake.generateId());

        if (!"1".equals(evidenceRecognitionRequest.getRecognitionType())) {
            evidenceRecognitionDO.setRecognitionStatus("1");
        } else {
            evidenceRecognitionDO.setRecognitionStatus("-1");
        }
        //单个上传
        if ("4".equals(evidenceRecognitionRequest.getRecognitionType())) {
            evidenceRecognitionMapper.insertEvidenceMapperDetail(snowFlake.generateId(), evidenceRecognitionDO.getId(), "contractId", evidenceRecognitionRequest.getContractId());
        } else if ("1".equals(evidenceRecognitionDO.getContinued())) {
            //兼容后续数据
            //识别类型自定义直接匹配条件
            if ("2".equals(evidenceRecognitionRequest.getRecognitionType())) {
                evidenceRecognitionMapper.insertEvidenceMapperDetail(snowFlake.generateId(), evidenceRecognitionDO.getId(), evidenceRecognitionRequest.getDsKey(), evidenceRecognitionRequest.getDsValue());
            }
        } else {
            //不兼容后续数据并且不是单个上传并且不是关键字
            if ("2".equals(evidenceRecognitionRequest.getRecognitionType()) || "3".equals(evidenceRecognitionRequest.getRecognitionType())) {
                //不兼容后续数据
                //识别类型自定义直接匹配条件
                //改为单个合同上传
                evidenceRecognitionDO.setRecognitionType(4);
                List<Map<String, String>> mapList = businessDataManageMapper.queryDataDetail(evidenceRecognitionRequest.getAssetInstBatchId());
                List<Map<String, Object>> queryResult = businessDataManageMapper.queryResult(evidenceRecognitionRequest.getAssetInstBatchId(), mapList);
                List<String> successIdList;
                if ("2".equals(evidenceRecognitionRequest.getRecognitionType())) {
                    //查询别名和昵称下的数据
                    successIdList = queryResult.stream().filter(query -> evidenceRecognitionRequest.getDsValue().equals(query.get(evidenceRecognitionRequest.getDsKey()))).map(query -> (String) query.get("contractId")).collect(Collectors.toList());
                } else {
                    successIdList = queryResult.stream().map(query -> (String) query.get("contractId")).collect(Collectors.toList());
                }
                if (CollectionUtil.isNotEmpty(successIdList)) {
                    for (String contractId : successIdList) {
                        evidenceRecognitionMapper.insertEvidenceMapperDetail(snowFlake.generateId(), evidenceRecognitionDO.getId(), "contractId", contractId);
                    }
                } else {
                    //识别不到的证据
                    evidenceRecognitionDO.setRecognitionStatus("0");
                }
            }
        }

        evidenceRecognitionMapper.insertEvidenceMapper(evidenceRecognitionDO);
        return true;
    }
}
